package com.anrongtec.zcpt.util;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;

import com.anrongtec.zcpt.R;

@SuppressLint("NewApi")
public class NotifyManager {

	private static final int FLAG = Notification.FLAG_INSISTENT;
	int requestCode = (int) SystemClock.uptimeMillis();
	private int NOTIFICATION_ID;
	private Notification notification;
	private NotificationCompat.Builder cBuilder;
	private Notification.Builder nBuilder;
	private Context mContext;

	// 创建private static类实例
	private volatile static NotifyManager INSTANCE;
	// 同步锁
	private static Object INSTANCE_LOCK = new Object();
	private static NotificationManager mNotificationManager;

	private Map<Integer, ArrayList<String>> map;

	/**
	 * 使用单例模式创建--双重锁定
	 */
	public static NotifyManager getInstance(Context context) {
		if (INSTANCE == null)
			synchronized (INSTANCE_LOCK) {
				if (INSTANCE == null) {
					INSTANCE = new NotifyManager();
				}
				INSTANCE.init(context);
			}
		return INSTANCE;
	}

	private NotifyManager() {
	}

	/**
	 * 只初始化创建一次上下文对象 init
	 */
	public void init(Context context) {
		this.mContext = context.getApplicationContext();
		mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		cBuilder = new NotificationCompat.Builder(mContext);
	}

	// public void noticOpenAppMsg(String pkgName, String ticker, String title,
	// String msg, int iconId) {
	// Intent intent =
	// mContext.getPackageManager().getLaunchIntentForPackage(pkgName);
	// if (null == intent) {
	// return;
	// }
	// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
	// TaskStackBuilder stackBuidler = TaskStackBuilder.create(mContext);
	// stackBuidler.addNextIntent(intent);
	// PendingIntent pendingIntent = stackBuidler.getPendingIntent(0,
	// PendingIntent.FLAG_UPDATE_CURRENT);
	// setCompatBuilder(pendingIntent, R.drawable.ic_launcher, ticker, title,
	// msg, true, true, true);
	// Bitmap largeBitmap =
	// BitmapFactory.decodeResource(mContext.getResources(), iconId);
	// cBuilder.setLargeIcon(largeBitmap);
	// cBuilder.setSmallIcon(R.drawable.ic_launcher);
	// NOTIFICATION_ID = UUID.randomUUID().hashCode();// 不重复的消息ID，避免覆盖
	// sent();
	// }

	/**
	 * 打开APP传递消息通知
	 * 
	 * @param pkgName
	 *            包名
	 * @param ticker
	 *            提示
	 * @param title
	 *            标题
	 * @param msg
	 *            消息
	 * @param data
	 *            传递数据
	 * @param icon
	 *            图标
	 */
	public void noticOpenAppMsg(String pkgName, String ticker, String title, String msg, String data, Drawable icon) {
		Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(pkgName);
		if (null == intent) {
			return;
		}
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("data", data);
		TaskStackBuilder stackBuidler = TaskStackBuilder.create(mContext);
		stackBuidler.addNextIntent(intent);
		PendingIntent pendingIntent = stackBuidler.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
		setCompatBuilder(pendingIntent, R.drawable.ic_launcher, ticker, title, msg, true, true, true);
		if (null != icon) {
			cBuilder.setLargeIcon(((BitmapDrawable) icon).getBitmap());
		}
		cBuilder.setSmallIcon(R.drawable.ic_launcher);
		NOTIFICATION_ID = UUID.randomUUID().hashCode();// 不重复的消息ID，避免覆盖
		sent();
	}

	public void noticAppHandleMsg(String pkgName, String uri, int flag, String ticker, String title, String msg,
			String data, Drawable icon) {
		Intent intent = new Intent();
		if (flag != 0) {
			intent.setFlags(flag);
		}
		intent.putExtra("data", data);
		Uri u = Uri.parse(uri);
		intent.setData(u);
		PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1000, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		setCompatBuilder(pendingIntent, R.drawable.ic_launcher, ticker, title, msg, true, true, true);
		if (null != icon) {
			cBuilder.setLargeIcon(((BitmapDrawable) icon).getBitmap());
		}
		cBuilder.setSmallIcon(R.drawable.ic_launcher);
		NOTIFICATION_ID = UUID.randomUUID().hashCode();// 不重复的消息ID，避免覆盖
		sent();
	}

	private Notification noticNewMsg(int ID, PendingIntent pendingIntent, Bitmap largeIcon,
			ArrayList<String> messageList, String ticker, String title, String content, boolean sound, boolean vibrate,
			boolean lights) {
		setCompatBuilder(pendingIntent, R.drawable.ic_launcher, ticker, title, content, sound, vibrate, lights);
		// 将Ongoing设为true 那么notification将不能滑动删除
		// cBuilder.setOngoing(true);

		/**
		 * // 删除时 Intent deleteIntent = new Intent(mContext,
		 * DeleteService.class); int deleteCode = (int)
		 * SystemClock.uptimeMillis(); // 删除时开启一个服务 PendingIntent
		 * deletePendingIntent = PendingIntent.getService(mContext, deleteCode,
		 * deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		 * cBuilder.setDeleteIntent(deletePendingIntent);
		 **/

		// Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
		// largeIcon);
		cBuilder.setLargeIcon(largeIcon);
		cBuilder.setDefaults(Notification.DEFAULT_ALL);// 设置使用默认的声音
		// cBuilder.setVibrate(new long[]{0, 100, 200, 300});// 设置自定义的振动
		cBuilder.setAutoCancel(true);
		// 设置默认声音
		// if (EMChatOptions.getInstance().getNotifyRingUri() != null) {
		// cBuilder.setSound(EMChatOptions.getInstance().getNotifyRingUri());
		// }

		// 设置通知样式为收件箱样式,在通知中心中两指往外拉动，就能出线更多内容，但是很少见
		// cBuilder.setNumber(messageList.size());
		NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
		for (String msg : messageList) {
			inboxStyle.addLine(msg);
		}
		inboxStyle.setSummaryText("[" + messageList.size() + "条]" + title);
		cBuilder.setStyle(inboxStyle);
		sent();
		return notification;

	}

	/**
	 * 设置在顶部通知栏中的各种信息
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 */
	private void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String ticker, String title,
			String content, boolean sound, boolean vibrate, boolean lights) {
		// // 如果当前Activity启动在前台，则不开启新的Activity。
		// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		// //
		// 当设置下面PendingIntent.FLAG_UPDATE_CURRENT这个参数的时候，常常使得点击通知栏没效果，你需要给notification设置一个独一无二的requestCode
		// // 将Intent封装进PendingIntent中，点击通知的消息后，就会启动对应的程序
		// PendingIntent pIntent = PendingIntent.getActivity(mContext,
		// requestCode, intent, FLAG);
		cBuilder.setContentIntent(pendingIntent);// 该通知要启动的Intent
		cBuilder.setSmallIcon(smallIcon);// 设置顶部状态栏的小图标
		cBuilder.setTicker(ticker);// 在顶部状态栏中的提示信息
		cBuilder.setContentTitle(title);// 设置通知中心的标题
		cBuilder.setContentText(content);// 设置通知中心中的内容
		cBuilder.setWhen(System.currentTimeMillis());

		/*
		 * 将AutoCancel设为true后，当你点击通知栏的notification后，它会自动被取消消失,
		 * 不设置的话点击消息后也不清除，但可以滑动删除
		 */
		cBuilder.setAutoCancel(true);
		// 将Ongoing设为true 那么notification将不能滑动删除
		// notifyBuilder.setOngoing(true);
		/*
		 * 从Android4.1开始，可以通过以下方法，设置notification的优先级，
		 * 优先级越高的，通知排的越靠前，优先级低的，不会在手机最顶部的状态栏显示图标
		 */
		cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
		/*
		 * Notification.DEFAULT_ALL：铃声、闪光、震动均系统默认。
		 * Notification.DEFAULT_SOUND：系统默认铃声。
		 * Notification.DEFAULT_VIBRATE：系统默认震动。
		 * Notification.DEFAULT_LIGHTS：系统默认闪光。
		 * notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
		 */
		int defaults = 0;

		if (sound) {
			defaults |= Notification.DEFAULT_SOUND;
		}
		if (vibrate) {
			defaults |= Notification.DEFAULT_VIBRATE;
		}
		if (lights) {
			defaults |= Notification.DEFAULT_LIGHTS;
		}
		cBuilder.setDefaults(defaults);
	}

	/**
	 * 进行多项设置的通知(在小米上似乎不能设置大图标，系统默认大图标为应用图标)
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 * @param title
	 * @param content
	 */
	private void notify_mailbox(PendingIntent pendingIntent, int smallIcon, int largeIcon,
			ArrayList<String> messageList, String ticker, String title, String content, boolean sound, boolean vibrate,
			boolean lights) {

		setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);

		// 将Ongoing设为true 那么notification将不能滑动删除
		// cBuilder.setOngoing(true);

		/**
		 * // 删除时 Intent deleteIntent = new Intent(mContext,
		 * DeleteService.class); int deleteCode = (int)
		 * SystemClock.uptimeMillis(); // 删除时开启一个服务 PendingIntent
		 * deletePendingIntent = PendingIntent.getService(mContext, deleteCode,
		 * deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		 * cBuilder.setDeleteIntent(deletePendingIntent);
		 **/

		Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);
		cBuilder.setLargeIcon(bitmap);

		cBuilder.setDefaults(Notification.DEFAULT_ALL);// 设置使用默认的声音
		// cBuilder.setVibrate(new long[]{0, 100, 200, 300});// 设置自定义的振动
		cBuilder.setAutoCancel(true);
		// builder.setSound(Uri.parse("file:///sdcard/click.mp3"));

		// 设置通知样式为收件箱样式,在通知中心中两指往外拉动，就能出线更多内容，但是很少见
		// cBuilder.setNumber(messageList.size());
		NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
		for (String msg : messageList) {
			inboxStyle.addLine(msg);
		}
		inboxStyle.setSummaryText("[" + messageList.size() + "条]" + title);
		cBuilder.setStyle(inboxStyle);
		sent();
	}

	/**
	 * 设置builder的信息，在用大文本时会用到这个
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 */
	// private void setBuilder(PendingIntent pendingIntent, int smallIcon,
	// String ticker, boolean sound, boolean vibrate, boolean lights) {
	// // 如果当前Activity启动在前台，则不开启新的Activity。
	// // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
	// // PendingIntent pIntent = PendingIntent.getActivity(mContext,
	// // requestCode, intent, FLAG);
	// nBuilder.setContentIntent(pendingIntent);
	//
	// nBuilder.setSmallIcon(smallIcon);
	//
	// nBuilder.setTicker(ticker);
	// nBuilder.setWhen(System.currentTimeMillis());
	// nBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
	//
	// int defaults = 0;
	//
	// if (sound) {
	// defaults |= Notification.DEFAULT_SOUND;
	// }
	// if (vibrate) {
	// defaults |= Notification.DEFAULT_VIBRATE;
	// }
	// if (lights) {
	// defaults |= Notification.DEFAULT_LIGHTS;
	// }
	//
	// nBuilder.setDefaults(defaults);
	// }

	/**
	 * 普通的通知 ,不显示消息内容
	 * <p/>
	 * 1. 侧滑即消失，下拉通知菜单则在通知菜单显示
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 * @param title
	 * @param content
	 */
	public void notifyScreenMsg(PendingIntent pendingIntent, int smallIcon, String ticker, String title, String content,
			boolean sound, boolean vibrate, boolean lights) {
		setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
		sent();
	}

	/**
	 * 自定义视图的通知
	 *
	 * @param remoteViews
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 */
	// public void notify_customview(RemoteViews remoteViews,
	// PendingIntent pendingIntent, int smallIcon, String ticker,
	// boolean sound, boolean vibrate, boolean lights) {
	//
	// setCompatBuilder(pendingIntent, smallIcon, ticker, null, null, sound,
	// vibrate, lights);
	//
	// notification = cBuilder.build();
	// notification.contentView = remoteViews;
	// // 发送该通知
	// nm.notify(NOTIFICATION_ID, notification);
	// }

	/**
	 * 可以容纳多行提示文本的通知信息 (因为在高版本的系统中才支持，所以要进行判断)
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 * @param title
	 * @param content
	 */
	// public void notify_normail_moreline(PendingIntent pendingIntent,
	// int smallIcon, String ticker, String title, String content,
	// boolean sound, boolean vibrate, boolean lights) {
	//
	// final int sdk = Build.VERSION.SDK_INT;
	// if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
	// notify_normal_singline(pendingIntent, smallIcon, ticker, title,
	// content, sound, vibrate, lights);
	// Toast.makeText(mContext, "您的手机低于Android 4.1.2，不支持多行通知显示！！",
	// Toast.LENGTH_SHORT).show();
	// } else {
	// setBuilder(pendingIntent, smallIcon, ticker, true, true, false);
	// nBuilder.setContentTitle(title);
	// nBuilder.setContentText(content);
	// nBuilder.setPriority(Notification.PRIORITY_HIGH);
	// notification = new Notification.BigTextStyle(nBuilder).bigText(
	// content).build();
	// // 发送该通知
	// mNotificationManager.notify(NOTIFICATION_ID, notification);
	// }
	// }

	/**
	 * 有进度条的通知，可以设置为模糊进度或者精确进度
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 * @param title
	 * @param content
	 */
	// public void notify_progress(PendingIntent pendingIntent, int smallIcon,
	// String ticker, String title, String content, boolean sound,
	// boolean vibrate, boolean lights) {
	//
	// setCompatBuilder(pendingIntent, smallIcon, ticker, title, content,
	// sound, vibrate, lights);
	// /*
	// * 因为进度条要实时更新通知栏也就说要不断的发送新的提示，所以这里不建议开启通知声音。
	// * 这里是作为范例，给大家讲解下原理。所以发送通知后会听到多次的通知声音。
	// */
	//
	// new Thread(new Runnable() {
	// @Override
	// public void run() {
	// int incr;
	// for (incr = 0; incr <= 100; incr += 10) {
	// // 参数：1.最大进度， 2.当前进度， 3.是否有准确的进度显示
	// cBuilder.setProgress(100, incr, false);
	// // cBuilder.setProgress(0, 0, true);
	// sent();
	// try {
	// Thread.sleep(1 * 500);
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	// // 进度满了后，设置提示信息
	// cBuilder.setContentText("下载完成").setProgress(0, 0, false);
	// sent();
	// }
	// }).start();
	// }

	/**
	 * 容纳大图片的通知
	 *
	 * @param pendingIntent
	 * @param smallIcon
	 * @param ticker
	 * @param title
	 * @param bigPic
	 */
	// public void notify_bigPic(PendingIntent pendingIntent, int smallIcon,
	// String ticker, String title, String content, int bigPic,
	// boolean sound, boolean vibrate, boolean lights) {
	//
	// setCompatBuilder(pendingIntent, smallIcon, ticker, title, null, sound,
	// vibrate, lights);
	// NotificationCompat.BigPictureStyle picStyle = new
	// NotificationCompat.BigPictureStyle();
	// final BitmapFactory.Options options = new BitmapFactory.Options();
	// options.inScaled = true;
	// options.inSampleSize = 2;
	// Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
	// bigPic, options);
	// picStyle.bigPicture(bitmap);
	// picStyle.bigLargeIcon(bitmap);
	// cBuilder.setContentText(content);
	// cBuilder.setStyle(picStyle);
	// sent();
	// }

	/**
	 * 里面有两个按钮的通知
	 *
	 * @param smallIcon
	 * @param leftbtnicon
	 * @param lefttext
	 * @param leftPendIntent
	 * @param rightbtnicon
	 * @param righttext
	 * @param rightPendIntent
	 * @param ticker
	 * @param title
	 * @param content
	 */
	private void notify_button(int smallIcon, int leftbtnicon, String lefttext, PendingIntent leftPendIntent,
			int rightbtnicon, String righttext, PendingIntent rightPendIntent, String ticker, String title,
			String content, boolean sound, boolean vibrate, boolean lights) {

		requestCode = (int) SystemClock.uptimeMillis();
		setCompatBuilder(rightPendIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
		cBuilder.addAction(leftbtnicon, lefttext, leftPendIntent);
		cBuilder.addAction(rightbtnicon, righttext, rightPendIntent);
		sent();
	}

	// private void notify_HeadUp(PendingIntent pendingIntent, int smallIcon,
	// int largeIcon, String ticker, String title, String content,
	// int leftbtnicon, String lefttext, PendingIntent leftPendingIntent,
	// int rightbtnicon, String righttext,
	// PendingIntent rightPendingIntent, boolean sound, boolean vibrate,
	// boolean lights) {
	//
	// setCompatBuilder(pendingIntent, smallIcon, ticker, title, content,
	// sound, vibrate, lights);
	// cBuilder.setLargeIcon(BitmapFactory.decodeResource(
	// mContext.getResources(), largeIcon));
	//
	// if (Build.VERSION.SDK_INT >= 21) {
	// cBuilder.addAction(leftbtnicon, lefttext, leftPendingIntent);
	// cBuilder.addAction(rightbtnicon, righttext, rightPendingIntent);
	// } else {
	// Toast.makeText(mContext, "版本低于Andriod5.0，无法体验HeadUp样式通知",
	// Toast.LENGTH_SHORT).show();
	// }
	// sent();
	// }

	/**
	 * 发送通知
	 */
	private void sent() {
		notification = cBuilder.build();
		// 发送该通知
		mNotificationManager.notify(NOTIFICATION_ID, notification);
	}

	/**
	 * 清空所有通知
	 */
	public void clear() {
		// 取消通知
		if (mNotificationManager != null) {
			mNotificationManager.cancelAll();
		}
		if (map != null) {
			map.clear();
		}
	}

	/**
	 * 根据id清除通知
	 */
	public void cancelNotifyById(int id) {
		if (mNotificationManager != null) {
			mNotificationManager.cancel(id);
		}
		if (map != null) {
			map.remove(id);
		}
	}
}
